
import readStream from '../utils/readStream.js';
import esbild from 'esbuild';
import imports from '../utils/Imports.js';
//重写ts和js的import路径
const RewritePlugin = ({ root, app }) => {
    app.use(async (ctx, next) => {
        await next()
        if (ctx.response.is('ts') || ctx.response.is('js')) {
            //流转换内容
            let body = await readStream(ctx.body)
            body = imports(body)
            //编译
            let result = esbild.transformSync(body, {
                loader: 'ts',
                target: 'es2015'
            })
            ctx.body = result.code
            ctx.type = 'js'
        }
    })
}
export default RewritePlugin